package openGl;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.util.Log;

import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import camera.OpenGl.utils.ByteBufferUtils;

/**
 * Created by Administrator on 2017/12/25.
 */

public class OpenGlGameRender implements GLSurfaceView.Renderer {
    private String Tag="OpenGlGameRender";
    /**
     * 顶点着色器
     */
    public  final String vertexShaderCode = "attribute vec4 a_Position;     \t\t\n" +
            "\n" +
            "void main()                    \n" +
            "{                              \n" +
            "    gl_Position = a_Position;\n" +
            "    gl_PointSize = 10.0;\n" +
            "}   ";
    /**
     * 片元着色器
     */
    public  final String fragmentShaderCode ="precision mediump float; \n" +
            "      \t \t\t\t\t\t\t\t\t\n" +
            "uniform vec4 u_Color;          \t   \t\t\t\t\t\t\t\t\n" +
            "  \n" +
            "void main()                    \t\t\n" +
            "{                              \t\n" +
            "    gl_FragColor = u_Color;                                  \t\t\n" +
            "}";
    private float[] gamePointers = {
            // Triangle 1
            -0.5f, -0.5f,
            0.5f,  0.5f,
            -0.5f,  0.5f,

            // Triangle 2
            -0.5f, -0.5f,
            0.5f, -0.5f,
            0.5f,  0.5f,

            // Line 1
            -0.5f, 0f,
            0.5f, 0f
    };
    private int program;
    private FloatBuffer pointers;
    private int u_color;
    private int a_position;


    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        pointers = ByteBufferUtils.makeFloteByteBuffer(gamePointers);
        GLES20.glClearColor(0f,0f,0f,0.0f);
        int vertexShader = OpenGlUtils.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = OpenGlUtils.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
        Log.e(Tag,"fragmentShader:"+fragmentShader);
        program = GLES20.glCreateProgram();
        GLES20.glAttachShader(program, vertexShader);
        GLES20.glAttachShader(program, fragmentShader);
        GLES20.glLinkProgram(program);

        GLES20.glUseProgram(program);
        //获取顶点数据
        a_position = GLES20.glGetAttribLocation(program, "a_Position");
        //获取颜色数据
        u_color = GLES20.glGetAttribLocation(program, "u_Color");
        GLES20.glVertexAttribPointer(a_position,//绘制的图形类型
                2,//每个顶点连接的边数（每个顶点关联的分量数目）
                GLES20.GL_FLOAT,//数据类型
                false,
                0,
                pointers//数据源
        );
        GLES20.glEnableVertexAttribArray(a_position);

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        //清除屏幕颜色
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
//        //绘制正方形
        GLES20.glUniform4f(a_position, 1.0f, 1.0f, 1.0f, 1.0f);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);
        //绘制中间的分割线
        GLES20.glUniform4f(a_position, 1.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glDrawArrays(GLES20.GL_LINES, 6, 2);
       
    }
}
